/**
 * 
 */
package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import officeserver.log_error.UserException;
import officeserver.report.Appointment;
import officeserver.report.Treatment;
import officeserver.users.AccessLevel;
import officeserver.users.PersonalInfo;

import GUI.calendar.CalendarPanel;
import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;
import GUI.main.renderers.TreatmentListRenderer;

/**
 * @author mramsey3
 * 
 */
public class ReportDialog extends JDialog implements ActionListener,
	MouseListener {

    private static final long serialVersionUID = 1L;

    private JPanel titlePanel;
    private JPanel mainPanel;
    private JPanel buttonPanel;

    private JPanel upperPanel;
    private JPanel lowerPanel;

    private JPanel upperNorthPanel;
    private JPanel upperCenterPanel;

    private JPanel upperNorthLeftPanel;
    private JPanel upperNorthRightPanel;

    private JPanel patientPanel;
    private JPanel doctorPanel;

    private JLabel patientLabel;
    private JLabel doctorLabel;

    private PTextField patientField;
    private PTextField doctorField;

    private JPanel dayPanel;
    private JPanel timePanel;

    private JLabel dayLabel;
    private JLabel timeLabel;

    private PTextField dayField;
    private PTextField timeField;

    private JPanel symptomsPanel;
    private JTextArea symptomsTextArea;

    private JPanel lowerLeftPanel;
    private JPanel lowerRightPanel;

    private JPanel lowerLeftNorthPanel;
    private JScrollPane treatmentScrollPane;
    private JList treatmentList;
    private JPanel lowerLeftSouthPanel;

    private PButton createButton;
    private PButton deleteButton;

    private JPanel lowerRightNorthPanel;
    private JPanel lowerRightCenterPanel;

    private JPanel bPSPanel;
    private JLabel bPSLabel;
    private PTextField bPSField;

    private JPanel bPDPanel;
    private JLabel bPDLabel;
    private PTextField bPDField;

    private JPanel tempPanel;
    private JLabel tempLabel;
    private PTextField tempField;

    private JPanel pulsePanel;
    private JLabel pulseLabel;
    private PTextField pulseField;

    private JPanel weightPanel;
    private JLabel weightLabel;
    private PTextField weightField;

    private JPanel heightPanel;
    private JLabel heightLabel;
    private PTextField heightField;

    private Appointment appointment;

    private PButton closeButton;
    private PButton invoiceButton;

    private static final int WIDTH = 600;
    private static final int HEIGHT = 725;

    /**
     * @param appointment
     */
    public ReportDialog(Appointment appointment) {
	this.appointment = appointment;
	setLayout(new BorderLayout());
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - WIDTH) / 2;
	final int y = (screenSize.height - HEIGHT) / 2;
	setLocation(x, y);	setSize(new Dimension(WIDTH, HEIGHT));
	add(getTitlePanel(), BorderLayout.NORTH);
	add(getMainPanel(), BorderLayout.CENTER);
	add(getButtonPanel(), BorderLayout.SOUTH);
    }

    /**
     * @return
     */
    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("report:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(WIDTH,
		    ProjectConstants.HEADER_HEIGHT));
	    titlePanel.addMouseListener(this);
	}
	return titlePanel;
    }

    /**
     * @return
     */
    private JPanel getMainPanel() {
	if (mainPanel == null) {
	    mainPanel = new JPanel();
	    mainPanel.setLayout(new GridLayout(2, 1));
	    mainPanel.add(getUpperPanel());
	    mainPanel.add(getLowerPanel());
	    mainPanel.addMouseListener(this);
	}
	return mainPanel;
    }

    /**
     * @return
     */
    private JPanel getUpperPanel() {
	if (upperPanel == null) {
	    upperPanel = new JPanel();
	    upperPanel.setLayout(new BorderLayout());
	    upperPanel.add(getUpperNorthPanel(), BorderLayout.NORTH);
	    upperPanel.add(getUpperCenterPanel(), BorderLayout.CENTER);
	    upperPanel.addMouseListener(this);
	}
	return upperPanel;
    }

    /**
     * @return
     */
    private JPanel getUpperNorthPanel() {
	if (upperNorthPanel == null) {
	    upperNorthPanel = new JPanel();
	    upperNorthPanel.setLayout(new GridLayout());
	    upperNorthPanel.add(getUpperNorthLeftPanel());
	    upperNorthPanel.add(getUpperNorthRightPanel());
	    upperNorthPanel.addMouseListener(this);
	}
	return upperNorthPanel;
    }

    /**
     * @return
     */
    private JPanel getUpperNorthLeftPanel() {
	if (upperNorthLeftPanel == null) {
	    upperNorthLeftPanel = new JPanel();
	    upperNorthLeftPanel.setLayout(new BoxLayout(upperNorthLeftPanel,
		    BoxLayout.PAGE_AXIS));
	    upperNorthLeftPanel.add(getPatientPanel());
	    upperNorthLeftPanel.add(getDoctorPanel());
	    upperNorthLeftPanel.addMouseListener(this);
	}
	return upperNorthLeftPanel;
    }

    /**
     * @return
     */
    private JPanel getPatientPanel() {
	if (patientPanel == null) {
	    patientPanel = new JPanel();
	    patientPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    patientPanel.setLayout(new BorderLayout());
	    patientPanel.add(getPatientLabel(), BorderLayout.WEST);
	    patientPanel.add(getPatientField(), BorderLayout.CENTER);
	    patientPanel.addMouseListener(this);
	}
	return patientPanel;
    }

    /**
     * @return
     */
    private JLabel getPatientLabel() {
	if (patientLabel == null) {
	    patientLabel = new JLabel(ProjectConstants.CELLPAD + "patient:");
	    patientLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    patientLabel.addMouseListener(this);
	}
	return patientLabel;
    }

    /**
     * @return
     */
    private PTextField getPatientField() {
	if (patientField == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getPatientID(), AccessLevel.PATIENT);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    patientField = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    patientField.addMouseListener(this);
	    patientField.setEditable(false);
	}
	return patientField;

    }

    /**
     * @return
     */
    private JPanel getDoctorPanel() {
	if (doctorPanel == null) {
	    doctorPanel = new JPanel();
	    doctorPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
	    doctorPanel.setLayout(new BorderLayout());
	    doctorPanel.add(getDoctorLabel(), BorderLayout.WEST);
	    doctorPanel.add(getDoctorField(), BorderLayout.CENTER);
	    doctorPanel.addMouseListener(this);
	}
	return doctorPanel;
    }

    /**
     * @return
     */
    private JLabel getDoctorLabel() {
	if (doctorLabel == null) {
	    doctorLabel = new JLabel(ProjectConstants.CELLPAD + "doctor:");
	    doctorLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    doctorLabel.addMouseListener(this);
	}
	return doctorLabel;
    }

    /**
     * @return
     */
    private PTextField getDoctorField() {
	if (doctorField == null) {
	    try {
		MainClient.getConnection().userSearch(
			appointment.getDoctorID(), AccessLevel.DOCTOR);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    PersonalInfo pInfo = null;
	    try {
		pInfo = MainClient.getConnection().getSearchPersonalInfo();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    doctorField = new PTextField(pInfo.getFirstName() + " "
		    + pInfo.getLastName(), TextFieldType.LONG);
	    doctorField.setEditable(false);
	    doctorField.addMouseListener(this);
	}
	return doctorField;
    }

    /**
     * @return
     */
    private JPanel getUpperNorthRightPanel() {
	if (upperNorthRightPanel == null) {
	    upperNorthRightPanel = new JPanel();
	    upperNorthRightPanel.setLayout(new BoxLayout(upperNorthRightPanel,
		    BoxLayout.PAGE_AXIS));
	    upperNorthRightPanel.add(getDayPanel());
	    upperNorthRightPanel.add(getTimePanel());
	    upperNorthRightPanel.addMouseListener(this);
	}
	return upperNorthRightPanel;
    }

    /**
     * @return
     */
    private JPanel getDayPanel() {
	if (dayPanel == null) {
	    dayPanel = new JPanel();
	    dayPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    dayPanel.setLayout(new BorderLayout());
	    dayPanel.add(getDayLabel(), BorderLayout.WEST);
	    dayPanel.add(getDayField(), BorderLayout.CENTER);
	    dayPanel.addMouseListener(this);
	}
	return dayPanel;
    }

    /**
     * @return
     */
    private JLabel getDayLabel() {
	if (dayLabel == null) {
	    dayLabel = new JLabel(ProjectConstants.CELLPAD + "date:");
	    dayLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    dayLabel.addMouseListener(this);
	}
	return dayLabel;
    }

    /**
     * @return
     */
    private PTextField getDayField() {
	if (dayField == null) {
	    appointment.getDay();
	    GregorianCalendar gc = new GregorianCalendar(2011, 0, 1);
	    gc.roll(Calendar.DAY_OF_YEAR, appointment.getDay());
	    StringBuilder builder = new StringBuilder();
	    builder.append(CalendarPanel.getMonthMap().get(
		    gc.get(Calendar.MONTH)));
	    builder.append(" ");
	    builder.append(gc.get(Calendar.DAY_OF_MONTH));
	    builder.append(", ");
	    builder.append(gc.get(Calendar.YEAR));
	    dayField = new PTextField(builder.toString(), TextFieldType.LONG);
	    dayField.setEditable(false);
	    dayField.addMouseListener(this);
	}
	return dayField;
    }

    /**
     * @return
     */
    private JPanel getTimePanel() {
	if (timePanel == null) {
	    timePanel = new JPanel();
	    timePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
	    timePanel.setLayout(new BorderLayout());
	    timePanel.add(getTimeLabel(), BorderLayout.WEST);
	    timePanel.add(getTimeField(), BorderLayout.CENTER);
	    timePanel.addMouseListener(this);
	}
	return timePanel;
    }

    /**
     * @return
     */
    private JLabel getTimeLabel() {
	if (timeLabel == null) {
	    timeLabel = new JLabel(ProjectConstants.CELLPAD + "time:");
	    timeLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    timeLabel.addMouseListener(this);
	}
	return timeLabel;
    }

    /**
     * @return
     */
    private PTextField getTimeField() {
	if (timeField == null) {
	    String out = CalendarPanel.getTimeSlot(appointment.getTimeSlot());
	    timeField = new PTextField(out, TextFieldType.LONG);
	    timeField.setEditable(false);
	    timeField.addMouseListener(this);
	}
	return timeField;
    }

    /**
     * @return
     */
    private JPanel getUpperCenterPanel() {
	if (upperCenterPanel == null) {
	    upperCenterPanel = new JPanel();
	    upperCenterPanel.setBorder(new MatteBorder(4,4,2,4,ProjectConstants.BORDER_MAIN_COLOR));
	    upperCenterPanel.setLayout(new BorderLayout());
	    upperCenterPanel.add(getSymptomsPanel(), BorderLayout.NORTH);
	    upperCenterPanel.add(getSymptomsTextArea(), BorderLayout.CENTER);
	    upperCenterPanel.addMouseListener(this);
	}
	return upperCenterPanel;
    }

    /**
     * @return
     */
    private JPanel getSymptomsPanel() {
	if (symptomsPanel == null) {
	    symptomsPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
		    g.drawString("SYMPTOMS:", 10, getHeight() - 5);
		}
	    };
	    symptomsPanel.setPreferredSize(new Dimension(WIDTH,50));
	}
	return symptomsPanel;
    }

    /**
     * @return
     */
    private JTextArea getSymptomsTextArea() {
	if (symptomsTextArea == null) {
	    symptomsTextArea = new JTextArea("double-click to edit");
	    symptomsTextArea.setPreferredSize(new Dimension(WIDTH, 100));
	    symptomsTextArea.setBorder(new EmptyBorder(10, 10, 10, 10));
	    symptomsTextArea.setEditable(false);
	    symptomsTextArea.addMouseListener(this);
	}
	return symptomsTextArea;
    }

    /**
     * @return
     */
    private JPanel getLowerPanel() {
	if (lowerPanel == null) {
	    lowerPanel = new JPanel();
	    lowerPanel.setLayout(new GridLayout(1, 2));
	    lowerPanel.add(getLowerLeftPanel());
	    lowerPanel.add(getLowerRightPanel());
	}
	return lowerPanel;
    }

    /**
     * @return
     */
    private JPanel getLowerLeftPanel() {
	if (lowerLeftPanel == null) {
	    lowerLeftPanel = new JPanel();
	    lowerLeftPanel.setBorder(new MatteBorder(2, 4, 4, 2, ProjectConstants.BORDER_MAIN_COLOR));
	    lowerLeftPanel.setLayout(new BorderLayout());
	    lowerLeftPanel.add(getLowerLeftNorthPanel(), BorderLayout.NORTH);
	    lowerLeftPanel.add(getTreatmentScrollPane(), BorderLayout.CENTER);
	    lowerLeftPanel.add(getLowerLeftSouthPanel(), BorderLayout.SOUTH);
	}
	return lowerLeftPanel;
    }

    /**
     * @return
     */
    private JPanel getLowerLeftNorthPanel() {
	if (lowerLeftNorthPanel == null) {
	    lowerLeftNorthPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
		    g.drawString("TREATMENTS:", 10, getHeight() - 5);
		}
	    };
	    lowerLeftNorthPanel.setPreferredSize(new Dimension(WIDTH / 2, 50));
	}
	return lowerLeftNorthPanel;
    }

    /**
     * @return
     */
    private JScrollPane getTreatmentScrollPane() {
	if (treatmentScrollPane == null) {
	    treatmentScrollPane = new JScrollPane();
	    treatmentScrollPane.setPreferredSize(new Dimension(WIDTH / 2,
		    HEIGHT / 2));
	    treatmentScrollPane.setViewportView(getTreatmentList());
	}
	return treatmentScrollPane;
    }

    /**
     * @return
     */
    private JList getTreatmentList() {
	if (treatmentList == null) {
	    treatmentList = new JList(new TreatmentListModel());
	    treatmentList.setCellRenderer(new TreatmentListRenderer());
	    treatmentList.addMouseListener(new DoubleClickListener(
		    treatmentList));
	}
	return treatmentList;
    }

    /**
     * @return
     */
    private JPanel getLowerLeftSouthPanel() {
	if (lowerLeftSouthPanel == null) {
	    lowerLeftSouthPanel = new JPanel();
	    lowerLeftSouthPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    lowerLeftSouthPanel.add(getCreateButton(), gbc1);
	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    lowerLeftSouthPanel.add(Box.createHorizontalStrut(50), gbc2);
	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    lowerLeftSouthPanel.add(getDeleteButton(), gbc3);
	    lowerLeftSouthPanel.addMouseListener(this);
	    lowerLeftSouthPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
	}
	return lowerLeftSouthPanel;
    }

    /**
     * @return
     */
    private PButton getCreateButton() {
	if (createButton == null) {
	    createButton = new PButton("create", ButtonType.SHORT);
	    createButton.addActionListener(this);
	}
	return createButton;
    }

    /**
     * @return
     */
    private PButton getDeleteButton() {
	if (deleteButton == null) {
	    deleteButton = new PButton("delete", ButtonType.SHORT);
	    deleteButton.addActionListener(this);
	}
	return deleteButton;
    }

    /**
     * @return
     */
    private JPanel getLowerRightPanel() {
	if (lowerRightPanel == null) {
	    lowerRightPanel = new JPanel();
	    lowerRightPanel.setBorder(new MatteBorder(2, 2, 4, 4, ProjectConstants.BORDER_MAIN_COLOR));
	    lowerRightPanel.setLayout(new BorderLayout());
	    lowerRightPanel.add(getLowerRightNorthPanel(), BorderLayout.NORTH);
	    lowerRightPanel
		    .add(getLowerRightCenterPanel(), BorderLayout.CENTER);
	    lowerRightPanel.addMouseListener(this);
	}
	return lowerRightPanel;
    }

    /**
     * @return
     */
    private JPanel getLowerRightNorthPanel() {
	if (lowerRightNorthPanel == null) {
	    lowerRightNorthPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
		    g.drawString("VITAL SIGNS:", 10, getHeight() - 5);
		}
	    };
	    lowerRightNorthPanel.setPreferredSize(new Dimension(WIDTH / 2, 50));
	}
	return lowerRightNorthPanel;
    }

    /**
     * @return
     */
    private JPanel getLowerRightCenterPanel() {
	if (lowerRightCenterPanel == null) {
	    lowerRightCenterPanel = new JPanel();
	    lowerRightCenterPanel.setLayout(new BoxLayout(
		    lowerRightCenterPanel, BoxLayout.PAGE_AXIS));
	    lowerRightCenterPanel.add(getBPSPanel());
	    lowerRightCenterPanel.add(getBPDPanel());
	    lowerRightCenterPanel.add(getTempPanel());
	    lowerRightCenterPanel.add(getPulsePanel());
	    lowerRightCenterPanel.add(getWeightPanel());
	    lowerRightCenterPanel.add(getHeightPanel());
	}
	return lowerRightCenterPanel;
    }

    /**
     * @return
     */
    private JPanel getBPSPanel() {
	if (bPSPanel == null) {
	    bPSPanel = new JPanel();
	    bPSPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    bPSPanel.setLayout(new BorderLayout());
	    bPSPanel.add(getBPSLabel(), BorderLayout.WEST);
	    bPSPanel.add(getBPSField(), BorderLayout.CENTER);
	    bPSPanel.addMouseListener(this);
	}
	return bPSPanel;
    }

    /**
     * @return
     */
    private JLabel getBPSLabel() {
	if (bPSLabel == null) {
	    bPSLabel = new JLabel(ProjectConstants.CELLPAD + "bP systolic:");
	    bPSLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    bPSLabel.addMouseListener(this);
	}
	return bPSLabel;
    }

    /**
     * @return
     */
    private PTextField getBPSField() {
	if (bPSField == null) {
	    bPSField = new PTextField("enter blood pressure",
		    TextFieldType.LONG);
	}
	return bPSField;
    }

    /**
     * @return
     */
    private JPanel getBPDPanel() {
	if (bPDPanel == null) {
	    bPDPanel = new JPanel();
	    bPDPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    bPDPanel.setLayout(new BorderLayout());
	    bPDPanel.add(getBPDLabel(), BorderLayout.WEST);
	    bPDPanel.add(getBPDField(), BorderLayout.CENTER);
	    bPDPanel.addMouseListener(this);
	}
	return bPDPanel;
    }

    /**
     * @return
     */
    private JLabel getBPDLabel() {
	if (bPDLabel == null) {
	    bPDLabel = new JLabel(ProjectConstants.CELLPAD + "bP diastolic:");
	    bPDLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    bPDLabel.addMouseListener(this);
	}
	return bPDLabel;
    }

    /**
     * @return
     */
    private PTextField getBPDField() {
	if (bPDField == null) {
	    bPDField = new PTextField("enter blood pressure",
		    TextFieldType.LONG);
	}
	return bPDField;
    }

    /**
     * @return
     */
    private JPanel getTempPanel() {
	if (tempPanel == null) {
	    tempPanel = new JPanel();
	    tempPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    tempPanel.setLayout(new BorderLayout());
	    tempPanel.add(getTempLabel(), BorderLayout.WEST);
	    tempPanel.add(getTempField(), BorderLayout.CENTER);
	    tempPanel.addMouseListener(this);
	}
	return tempPanel;
    }

    /**
     * @return
     */
    private JLabel getTempLabel() {
	if (tempLabel == null) {
	    tempLabel = new JLabel(ProjectConstants.CELLPAD + "temperature:");
	    tempLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    tempLabel.addMouseListener(this);
	}
	return tempLabel;
    }

    /**
     * @return
     */
    private PTextField getTempField() {
	if (tempField == null) {
	    tempField = new PTextField("enter temperature", TextFieldType.LONG);
	}
	return tempField;
    }

    /**
     * @return
     */
    private JPanel getPulsePanel() {
	if (pulsePanel == null) {
	    pulsePanel = new JPanel();
	    pulsePanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    pulsePanel.setLayout(new BorderLayout());
	    pulsePanel.add(getPulseLabel(), BorderLayout.WEST);
	    pulsePanel.add(getPulseField(), BorderLayout.CENTER);
	    pulsePanel.addMouseListener(this);
	}
	return pulsePanel;
    }

    /**
     * @return
     */
    private JLabel getPulseLabel() {
	if (pulseLabel == null) {
	    pulseLabel = new JLabel(ProjectConstants.CELLPAD + "heart rate:");
	    pulseLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    pulseLabel.addMouseListener(this);
	}
	return pulseLabel;
    }

    /**
     * @return
     */
    private PTextField getPulseField() {
	if (pulseField == null) {
	    pulseField = new PTextField("enter heart rate", TextFieldType.LONG);
	}
	return pulseField;
    }

    /**
     * @return
     */
    private JPanel getWeightPanel() {
	if (weightPanel == null) {
	    weightPanel = new JPanel();
	    weightPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
	    weightPanel.setLayout(new BorderLayout());
	    weightPanel.add(getWeightLabel(), BorderLayout.WEST);
	    weightPanel.add(getWeightField(), BorderLayout.CENTER);
	    weightPanel.addMouseListener(this);
	}
	return weightPanel;
    }

    /**
     * @return
     */
    private JLabel getWeightLabel() {
	if (weightLabel == null) {
	    weightLabel = new JLabel(ProjectConstants.CELLPAD + "weight:");
	    weightLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    weightLabel.addMouseListener(this);
	}
	return weightLabel;
    }

    /**
     * @return
     */
    private PTextField getWeightField() {
	if (weightField == null) {
	    weightField = new PTextField("enter weight", TextFieldType.LONG);
	}
	return weightField;
    }

    /**
     * @return
     */
    private JPanel getHeightPanel() {
	if (heightPanel == null) {
	    heightPanel = new JPanel();
	    heightPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
	    heightPanel.setLayout(new BorderLayout());
	    heightPanel.add(getHeightLabel(), BorderLayout.WEST);
	    heightPanel.add(getHeightField(), BorderLayout.CENTER);
	    heightPanel.addMouseListener(this);
	}
	return heightPanel;
    }

    /**
     * @return
     */
    private JLabel getHeightLabel() {
	if (heightLabel == null) {
	    heightLabel = new JLabel(ProjectConstants.CELLPAD + "height:");
	    heightLabel.setPreferredSize(new Dimension(
		    ProjectConstants.LABEL_WIDTH_A, 25));
	    heightLabel.addMouseListener(this);
	}
	return heightLabel;
    }

    /**
     * @return
     */
    private PTextField getHeightField() {
	if (heightField == null) {
	    heightField = new PTextField("enter height", TextFieldType.LONG);
	}
	return heightField;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
	    buttonPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    buttonPanel.add(getCloseButton(), gbc1);
	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    buttonPanel.add(Box.createHorizontalStrut(50), gbc2);
	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    buttonPanel.add(getInvoiceButton(), gbc3);
	    buttonPanel.addMouseListener(this);
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private PButton getCloseButton() {
	if (closeButton == null) {
	    closeButton = new PButton("close", ButtonType.MEDIUM);
	    closeButton.addActionListener(this);
	}
	return closeButton;
    }

    /**
     * @return
     */
    private PButton getInvoiceButton() {
	if (invoiceButton == null) {
	    invoiceButton = new PButton("view invoice", ButtonType.MEDIUM);
	    invoiceButton.addActionListener(this);
	}
	return invoiceButton;
    }

    /**
     * 
     */
    public void refreshPanel() {
	treatmentList.updateUI();
	repaint();
    }

    /**
     * @author mramsey3
     *
     */
    private class TreatmentListModel extends AbstractListModel {
	private static final long serialVersionUID = 1L;

	public Object getElementAt(int i) {
	    if (appointment.getReport() != null) {
		return appointment.getReport().getTreatments().get(i);
	    }
	    return 0;
	}

	public int getSize() {
	    if (appointment.getReport() != null) {
		return appointment.getReport().getTreatments().size();
	    }
	    return 0;
	}
    }

    /**
     * @author mramsey3
     *
     */
    private class DoubleClickListener extends MouseAdapter {
	protected JList list;

	/**
	 * @param l
	 */
	public DoubleClickListener(JList l) {
	    list = l;
	}

	public void mouseClicked(MouseEvent e) {
	    if (e.getSource() == getTreatmentList())
		if (e.getClickCount() == 2) {
		    if (getTreatmentList().getModel().getSize() > 0) {
			int index = list.locationToIndex(e.getPoint());
			ListModel lm = list.getModel();
			Treatment t = (Treatment) (lm.getElementAt(index));
			refreshPanel();
		    }
		}
	}
    }

    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getCloseButton()) {
	    setVisible(false);
	}else if (e.getSource() == getCreateButton()){
	    AddTreatmentDialog temp = new AddTreatmentDialog(appointment);
	    temp.setVisible(true);
	}
    }

    public void mouseClicked(MouseEvent e) {
	if (e.getSource() == getSymptomsTextArea()) {
	    if (e.getClickCount() == 2) {
		getSymptomsTextArea().setEditable(true);
	    }
	} else {
	    getSymptomsTextArea().setEditable(false);
	}
    }

    // unused methods...
    public void mouseEntered(MouseEvent arg0) {
    }

    public void mouseExited(MouseEvent arg0) {
    }

    public void mousePressed(MouseEvent arg0) {
    }

    public void mouseReleased(MouseEvent arg0) {
    }

}
